﻿namespace Services
{
    public interface IJudgeWireropeStatusMonitor
    {
        CheckWireropeStatusMonitor JudgeWireropeMonitor(int usingWireropeID);
        void InsertOrUpdateWireropeMonitor(CheckWireropeStatusMonitor model);
    }

    public class JudgeWireropeStatusMonitor : IJudgeWireropeStatusMonitor
    {
        private readonly ISqlSugarClient _DB;
        private readonly ICurrentUser _currentUser;
        private readonly ICheckWireropeRepository _ICheckWireropeRepository;

        public JudgeWireropeStatusMonitor(ISqlSugarClient dB, ICurrentUser currentUser, ICheckWireropeRepository iCheckWireropeRepository)
        {
            _DB = dB;
            _currentUser = currentUser;
            _ICheckWireropeRepository = iCheckWireropeRepository;
        }

        public void InsertOrUpdateWireropeMonitor(CheckWireropeStatusMonitor model)
        {
            //判断当前数据是否有数据，如果有数据就是更新，如果没有数据就是新增
            var list = _DB.Queryable<CheckWireropeStatusMonitor>().Where(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID && x.UsingWireropeID == model.UsingWireropeID).ToList();

            if (list.Count > 0)
            {
                var temp = list.First();
                model.CheckWireropeStatusMonitorID = temp.CheckWireropeStatusMonitorID;
                _DB.Updateable(model).ExecuteCommand();
            }
            else
            {
                model.GroupInfoID = _currentUser.CurrentUserInfo.LoginTypeID;
                _DB.Insertable(model).ExecuteCommand();
            }
        }

        public CheckWireropeStatusMonitor JudgeWireropeMonitor(int usingWireropeID)
        {
            //获取当前集团中的标准信息
            var standStatus = _DB.Queryable<StandStatus>().Where(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID).ToList();
            var standStatusDetails = _DB.Queryable<StandStatusDetail>().Where(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID).ToList();

            //构建基础的检测状态对象
            CheckWireropeStatusMonitor info = new CheckWireropeStatusMonitor();
            info.GroupInfoID = _currentUser.CurrentUserInfo.LoginTypeID;
            info.UsingWireropeID = usingWireropeID;

            //得到在用绳信息
            var usingWirerope = _DB.Queryable<UsingWirerope>().Where(x => x.UsingWireropeID == usingWireropeID).First();

            //得到最后一次检验信息，状态为已检测的
            var lastCheck = _ICheckWireropeRepository.GetLastCheckWirerope(usingWireropeID);

            if (lastCheck != null)
            {
                //得到最后一次检测的详情
                var checkDetails = _DB.Queryable<CheckWireropeDetail>().Where(x => x.CheckWireropeID == lastCheck.CheckWireropeID).ToList();

                //得到每个数据的最大值
                var LAMax = checkDetails.Where(x => x.DsLa.HasValue).Max(x => x.DsLa.Value);
                var DIAMax = checkDetails.Where(x => x.ZjDia.HasValue).Max(x => x.ZjDia.Value);
                var LMAMax = checkDetails.Where(x => x.JsjmLma.HasValue).Max(x => x.JsjmLma.Value);
                var NianJuMax = checkDetails.Where(x => x.NianJuChange.HasValue).Max(x => x.NianJuChange.Value);

                info.LaVal = LAMax;
                info.DiaVal = DIAMax;
                info.LmaVal = LMAMax;
                info.NianJuVal = NianJuMax;

                //LA
                var LAStandStatus = standStatus.FirstOrDefault(x => x.Type == (int)StandStatus_Type.LA);
                StandStatusDetail LAStandStatusDetail = null;
                if (LAStandStatus != null)
                {
                    LAStandStatusDetail = standStatusDetails.Where(x => x.StandStatusID == LAStandStatus.StandStatusID && x.HoisterID == usingWirerope.HoisterID && x.HoisterUsageID == usingWirerope.HoisterUsageID && x.WireropeTypeID == usingWirerope.WireropeTypeID).FirstOrDefault();

                    if (LAStandStatusDetail != null)
                    {
                        if (LAMax >= 0 && LAMax < LAStandStatusDetail.GreenLight)
                        {
                            info.La = (int)StatusMonitor_LightType.绿;
                        }
                        else if (LAMax >= LAStandStatusDetail.GreenLight && LAMax < LAStandStatusDetail.BlueLight)
                        {
                            info.La = (int)StatusMonitor_LightType.蓝;
                        }
                        else if (LAMax >= LAStandStatusDetail.BlueLight && LAMax < LAStandStatusDetail.YellowLight)
                        {
                            info.La = (int)StatusMonitor_LightType.黄;
                        }
                        else if (LAMax >= LAStandStatusDetail.YellowLight && LAMax < LAStandStatusDetail.OrangeLight)
                        {
                            info.La = (int)StatusMonitor_LightType.橙;
                        }
                        else
                        {
                            info.La = (int)StatusMonitor_LightType.灰;
                        }
                    }
                }

                //DIA
                var DIAStandStatus = standStatus.FirstOrDefault(x => x.Type == (int)StandStatus_Type.DIA);
                StandStatusDetail DIAStandStatusDetail = null;
                if (DIAStandStatus != null)
                {
                    DIAStandStatusDetail = standStatusDetails.Where(x => x.StandStatusID == DIAStandStatus.StandStatusID && x.HoisterID == usingWirerope.HoisterID && x.HoisterUsageID == usingWirerope.HoisterUsageID && x.WireropeTypeID == usingWirerope.WireropeTypeID).FirstOrDefault();

                    if (DIAStandStatusDetail != null)
                    {
                        if (DIAMax < DIAStandStatusDetail.GreenLight)
                        {
                            info.Dia = (int)StatusMonitor_LightType.绿;
                        }
                        else if (DIAMax >= DIAStandStatusDetail.GreenLight && DIAMax < DIAStandStatusDetail.BlueLight)
                        {
                            info.Dia = (int)StatusMonitor_LightType.蓝;
                        }
                        else if (DIAMax >= DIAStandStatusDetail.BlueLight && DIAMax < DIAStandStatusDetail.YellowLight)
                        {
                            info.Dia = (int)StatusMonitor_LightType.黄;
                        }
                        else if (DIAMax >= DIAStandStatusDetail.YellowLight && DIAMax < DIAStandStatusDetail.OrangeLight)
                        {
                            info.Dia = (int)StatusMonitor_LightType.橙;
                        }
                        else
                        {
                            info.Dia = (int)StatusMonitor_LightType.灰;
                        }
                    }
                }

                //LMA
                var LMAStandStatus = standStatus.FirstOrDefault(x => x.Type == (int)StandStatus_Type.LMA);
                if (LMAStandStatus != null)
                {
                    var LMAStandStatusDetail = standStatusDetails.Where(x => x.StandStatusID == LMAStandStatus.StandStatusID && x.HoisterID == usingWirerope.HoisterID && x.HoisterUsageID == usingWirerope.HoisterUsageID && x.WireropeTypeID == usingWirerope.WireropeTypeID).FirstOrDefault();

                    if (LMAStandStatusDetail != null)
                    {
                        if (LMAMax >= 0 && LMAMax < LMAStandStatusDetail.GreenLight)
                        {
                            info.Lma = (int)StatusMonitor_LightType.绿;
                        }
                        else if (LMAMax >= LMAStandStatusDetail.GreenLight && LMAMax < LMAStandStatusDetail.BlueLight)
                        {
                            info.Lma = (int)StatusMonitor_LightType.蓝;
                        }
                        else if (LMAMax >= LMAStandStatusDetail.BlueLight && LMAMax < LMAStandStatusDetail.YellowLight)
                        {
                            info.Lma = (int)StatusMonitor_LightType.黄;
                        }
                        else if (LMAMax >= LMAStandStatusDetail.YellowLight && LMAMax < LMAStandStatusDetail.OrangeLight)
                        {
                            info.Lma = (int)StatusMonitor_LightType.橙;
                        }
                        else
                        {
                            info.Lma = (int)StatusMonitor_LightType.灰;
                        }
                    }
                }

                //捻距变化
                var NianJuStandStatus = standStatus.FirstOrDefault(x => x.Type == (int)StandStatus_Type.捻距变化);
                if (NianJuStandStatus != null)
                {
                    var NianJuStandStatusDetail = standStatusDetails.Where(x => x.StandStatusID == NianJuStandStatus.StandStatusID && x.HoisterID == usingWirerope.HoisterID && x.HoisterUsageID == usingWirerope.HoisterUsageID && x.WireropeTypeID == usingWirerope.WireropeTypeID).FirstOrDefault();

                    if (NianJuStandStatusDetail != null)
                    {
                        if (NianJuMax >= 0 && NianJuMax < NianJuStandStatusDetail.GreenLight)
                        {
                            info.NianJu = (int)StatusMonitor_LightType.绿;
                        }
                        else if (NianJuMax >= NianJuStandStatusDetail.GreenLight && NianJuMax < NianJuStandStatusDetail.BlueLight)
                        {
                            info.NianJu = (int)StatusMonitor_LightType.蓝;
                        }
                        else if (NianJuMax >= NianJuStandStatusDetail.BlueLight && NianJuMax < NianJuStandStatusDetail.YellowLight)
                        {
                            info.NianJu = (int)StatusMonitor_LightType.黄;
                        }
                        else if (NianJuMax >= NianJuStandStatusDetail.YellowLight && NianJuMax < NianJuStandStatusDetail.OrangeLight)
                        {
                            info.NianJu = (int)StatusMonitor_LightType.橙;
                        }
                        else
                        {
                            info.NianJu = (int)StatusMonitor_LightType.灰;
                        }
                    }
                }

                //综合
                if (LAStandStatusDetail != null && DIAStandStatusDetail != null && LAStandStatusDetail.OrangeLight.HasValue && DIAStandStatusDetail.OrangeLight.HasValue)
                {
                    var LAp = LAStandStatusDetail.OrangeLight.Value;
                    var DIAp = DIAStandStatusDetail.OrangeLight.Value;

                    //通过综合状态的计算公式，DF=LA/LAp +DIA/DIAp ，计算综合状态的值
                    var DF = Math.Round(LAMax / LAp + DIAMax / DIAp, 2);
                    info.SummaryVal = DF;

                    //根据基础数据(通过提升机类型，提升用途，钢丝绳类型)，获得综合是什么状态
                    var DFStandStatus = standStatus.FirstOrDefault(x => x.Type == (int)StandStatus_Type.综合);

                    if (DFStandStatus != null)
                    {
                        var DFStandStatusDetail = standStatusDetails.Where(x => x.StandStatusID == DFStandStatus.StandStatusID && x.HoisterID == usingWirerope.HoisterID && x.HoisterUsageID == usingWirerope.HoisterUsageID && x.WireropeTypeID == usingWirerope.WireropeTypeID).FirstOrDefault();

                        if (DFStandStatusDetail != null)
                        {
                            if (DF >= 0 && DF < DFStandStatusDetail.GreenLight)
                            {
                                info.Summary = (int)StatusMonitor_LightType.绿;
                            }
                            else if (DF >= DFStandStatusDetail.GreenLight && DF < DFStandStatusDetail.BlueLight)
                            {
                                info.Summary = (int)StatusMonitor_LightType.蓝;
                            }
                            else if (DF >= DFStandStatusDetail.BlueLight && DF < DFStandStatusDetail.YellowLight)
                            {
                                info.Summary = (int)StatusMonitor_LightType.黄;
                            }
                            else if (DF >= DFStandStatusDetail.YellowLight && DF < DFStandStatusDetail.OrangeLight)
                            {
                                info.Summary = (int)StatusMonitor_LightType.橙;
                            }
                            else
                            {
                                info.Summary = (int)StatusMonitor_LightType.灰;
                            }
                        }
                    }
                }
            }

            return info;
        }
    }
}
